package com.sofwin.service.impl;

import com.sofwin.mapper.SysDeptMapper;
import com.sofwin.pojo.SysDept;
import com.sofwin.pojo.SysDeptExample;
import com.sofwin.system.DeptService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @packageName: com.sofwin.service.impl
 * @user: andyliu
 * @date: 2021/9/23 11:46
 * @email 115176513@qq.com
 * @description: TODO
 */
@Service(interfaceClass = DeptService.class,timeout = 10000)
public class DeptServiceImpl implements DeptService {

    @Autowired
    private SysDeptMapper mapper;

    @Override
    public List<SysDept> queryDepts(SysDept dept) {
        SysDeptExample ex = new SysDeptExample();
        SysDeptExample.Criteria criteria = ex.createCriteria();
        if(dept!=null){
            if(!StringUtils.isEmpty(dept.getDeptName())){
                criteria.andDeptNameLike("%"+dept.getDeptName()+"%");
            }
            if(dept.getStatus()!=null && dept.getStatus()!=-1){
                criteria.andStatusEqualTo(dept.getStatus());
            }
        }
        List<SysDept> depts = mapper.selectByExample(ex);
        return depts;
    }

    @Override
    public SysDept queryById(Integer deptId) {
        return mapper.selectByPrimaryKey(deptId);
    }

    @Override
    public boolean saveOrUpdate(SysDept dept) {
        if(dept.getDeptId()!=null){
            return mapper.updateByPrimaryKeySelective(dept)==1?true:false;
        }
        return mapper.insertSelective(dept)==1?true:false;
    }

    @Override
    public boolean removeDepts(Integer[] ids) {
        int num=0;
        for(Integer id:ids){
            num+=mapper.deleteByPrimaryKey(id);
        }
        return num==ids.length?true:false;
    }
}
